<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            background: url('../images/login.jpg') no-repeat center center / cover;
        }

        .login-box {
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        .login-box h2 {
            text-align: center;
            color: #ff6666;
            margin-bottom: 20px;
        }

        .form-item {
            margin-bottom: 15px;
        }

        .form-item label {
            display: block;
            margin-bottom: 5px;
            color: #666;
        }

        .form-item input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        .error-tip {
            color: red;
            text-align: center;
            margin: 10px 0;
            display: none;
        }

        .login-btn {
            width: 100%;
            padding: 10px;
            background-color: #ff6666;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .login-btn:hover {
            background-color: #e05555;
        }
    </style>
    <title>用户登录</title>
</head>

<body>
    <div class="login-box">
        <h2>用户登录</h2>
        <div class="form-item">
            <label for="username">用户名：</label>
            <input type="text" id="username" placeholder="请输入用户名">
        </div>
        <div class="form-item">
            <label for="password">密码：</label>
            <input type="password" id="password" placeholder="请输入密码">
        </div>
        <p class="error-tip" id="errorTip">用户名或密码不符合要求，请重新输入</p>
        <button class="login-btn" id="loginBtn">登录</button>
    </div>

    <script>
        const loginBtn = document.getElementById('loginBtn');
        const usernameInput = document.getElementById('username');
        const passwordInput = document.getElementById('password');
        const errorTip = document.getElementById('errorTip');

        const usernameReg = /^[a-zA-Z0-9\u4e00-\u9fa5]{2,10}$/;
        const passwordReg = /^[\w]{6,16}$/;

        loginBtn.addEventListener('click', function () {
            const username = usernameInput.value.trim();
            const password = passwordInput.value.trim();
            let isValid = true;
            let errorMessage = '';

            if (!usernameReg.test(username)) {
                isValid = false;
                errorMessage = '用户名格式不正确，请输入2到10位的字母、数字或汉字。';
            }
            else if (!passwordReg.test(password)) {
                isValid = false;
                errorMessage = '密码格式不正确，请输入6到16位的字母、数字或下划线。';
            }

            if (isValid) {
                localStorage.setItem('username', username);
                window.location.href = 'index.html';
            } else {
                errorTip.textContent = errorMessage;
                errorTip.style.display = 'block';

                setTimeout(() => {
                    errorTip.style.display = 'none';
                }, 3000);
            }
        });

        usernameInput.addEventListener('input', () => {
            errorTip.style.display = 'none';
        });

        passwordInput.addEventListener('input', () => {
            errorTip.style.display = 'none';
        });

    </script>
</body>

</html>